package com.blkj.iam.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blkj.iam.common.constant.SsoUrlConstants;
import com.blkj.iam.common.constant.SyncDataConstants;
import com.blkj.iam.config.property.SyncDataProperties;
import com.blkj.iam.core.security.util.SecurityUtils;
import com.blkj.iam.system.mapper.SysSyncDataMapper;
import com.blkj.iam.system.model.entity.Client;
import com.blkj.iam.system.model.entity.SysSyncData;
import com.blkj.iam.system.service.ClientService;
import com.blkj.iam.system.service.SysSyncDataService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @description: 数据同步接口实现类
 * @author: Ethan
 * @create: 2025-05-14
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class SysSyncDataServiceImpl extends ServiceImpl<SysSyncDataMapper, SysSyncData> implements SysSyncDataService {

    private final ClientService clientService;
    private final SyncDataProperties dataProperties;


    @Override
    public void saveUserAdd(String jsonStr, String error) {
        SysSyncData sysSyncData = buildEntity(jsonStr, error);
        sysSyncData.setRemark(SyncDataConstants.USER_ADD);
        sysSyncData.setUrl(SsoUrlConstants.ADD_USER);
        List<String> excludeAppIds = dataProperties.getExcludeAppIds();
        if (!excludeAppIds.contains(sysSyncData.getAppId())) {
            this.save(sysSyncData);
        }
    }

    @Override
    public void saveUserEdit(String jsonStr, String error) {
        SysSyncData sysSyncData = buildEntity(jsonStr, error);
        sysSyncData.setRemark(SyncDataConstants.USER_EDIT);
        sysSyncData.setUrl(SsoUrlConstants.EDIT_USER);
        List<String> excludeAppIds = dataProperties.getExcludeAppIds();
        if (!excludeAppIds.contains(sysSyncData.getAppId())) {
            this.save(sysSyncData);
        }
    }

    @Override
    public void saveUserDel(String jsonStr, String error) {
        SysSyncData sysSyncData = buildEntity(jsonStr, error);
        sysSyncData.setRemark(SyncDataConstants.USER_DEL);
        sysSyncData.setUrl(SsoUrlConstants.DEl_USER);
        List<String> excludeAppIds = dataProperties.getExcludeAppIds();
        if (!excludeAppIds.contains(sysSyncData.getAppId())) {
            this.save(sysSyncData);
        }
    }

    @Override
    public void saveRoleAdd(String jsonStr, String error) {
        SysSyncData sysSyncData = buildEntity(jsonStr, error);
        sysSyncData.setRemark(SyncDataConstants.ROLE_ADD);
        sysSyncData.setUrl(SsoUrlConstants.ADD_ROLE);
        List<String> excludeAppIds = dataProperties.getExcludeAppIds();
        if (!excludeAppIds.contains(sysSyncData.getAppId())) {
            this.save(sysSyncData);
        }
    }

    @Override
    public void saveRoleEdit(String jsonStr, String error) {
        SysSyncData sysSyncData = buildEntity(jsonStr, error);
        sysSyncData.setRemark(SyncDataConstants.ROLE_EDIT);
        sysSyncData.setUrl(SsoUrlConstants.EDIT_ROLE);
        List<String> excludeAppIds = dataProperties.getExcludeAppIds();
        if (!excludeAppIds.contains(sysSyncData.getAppId())) {
            this.save(sysSyncData);
        }
    }

    @Override
    public void saveRoleDel(String jsonStr, String error) {
        SysSyncData sysSyncData = buildEntity(jsonStr, error);
        sysSyncData.setRemark(SyncDataConstants.ROLE_DEL);
        sysSyncData.setUrl(SsoUrlConstants.DEl_ROLE);
        List<String> excludeAppIds = dataProperties.getExcludeAppIds();
        if (!excludeAppIds.contains(sysSyncData.getAppId())) {
            this.save(sysSyncData);
        }
    }

    @Override
    public void saveDeptAdd(String jsonStr, String error) {
        SysSyncData sysSyncData = buildEntity(jsonStr, error);
        sysSyncData.setRemark(SyncDataConstants.DEPT_ADD);
        sysSyncData.setUrl(SsoUrlConstants.ADD_DEPT);
        List<String> excludeAppIds = dataProperties.getExcludeAppIds();
        if (!excludeAppIds.contains(sysSyncData.getAppId())) {
            this.save(sysSyncData);
        }
    }

    @Override
    public void saveDeptEdit(String jsonStr, String error) {
        SysSyncData sysSyncData = buildEntity(jsonStr, error);
        sysSyncData.setRemark(SyncDataConstants.DEPT_EDIT);
        sysSyncData.setUrl(SsoUrlConstants.EDIT_DEPT);
        List<String> excludeAppIds = dataProperties.getExcludeAppIds();
        if (!excludeAppIds.contains(sysSyncData.getAppId())) {
            this.save(sysSyncData);
        }
    }

    @Override
    public void saveDeptDel(String jsonStr, String error) {
        SysSyncData sysSyncData = buildEntity(jsonStr, error);
        sysSyncData.setRemark(SyncDataConstants.DEPT_DEL);
        sysSyncData.setUrl(SsoUrlConstants.DEL_DEPT);
        List<String> excludeAppIds = dataProperties.getExcludeAppIds();
        if (!excludeAppIds.contains(sysSyncData.getAppId())) {
            this.save(sysSyncData);
        }
    }

    @Override
    public void saveMenuAdd(String jsonStr, String error) {
        SysSyncData sysSyncData = buildEntity(jsonStr, error);
        sysSyncData.setRemark(SyncDataConstants.MENU_ADD);
        sysSyncData.setUrl(SsoUrlConstants.ADD_MENU);
        List<String> excludeAppIds = dataProperties.getExcludeAppIds();
        if (!excludeAppIds.contains(sysSyncData.getAppId())) {
            this.save(sysSyncData);
        }
    }

    @Override
    public void saveMenuEdit(String jsonStr, String error) {
        SysSyncData sysSyncData = buildEntity(jsonStr, error);
        sysSyncData.setRemark(SyncDataConstants.MENU_EDIT);
        sysSyncData.setUrl(SsoUrlConstants.EDIT_MENU);
        List<String> excludeAppIds = dataProperties.getExcludeAppIds();
        if (!excludeAppIds.contains(sysSyncData.getAppId())) {
            this.save(sysSyncData);
        }
    }

    @Override
    public void saveMenuDel(String jsonStr, String error) {
        SysSyncData sysSyncData = buildEntity(jsonStr, error);
        sysSyncData.setRemark(SyncDataConstants.MENU_DEL);
        sysSyncData.setUrl(SsoUrlConstants.DEL_MENU);
        List<String> excludeAppIds = dataProperties.getExcludeAppIds();
        if (!excludeAppIds.contains(sysSyncData.getAppId())) {
            this.save(sysSyncData);
        }
    }

    private SysSyncData buildEntity(String jsonStr, String error) {
        SysSyncData sysSyncData = new SysSyncData();
        sysSyncData.setCreateUser(SecurityUtils.getUsername());
        Long clientId = SecurityUtils.getClientId();
        Client client = clientService.getById(clientId);
        sysSyncData.setAppId(client.getAppId());
        sysSyncData.setAppName(client.getAppName());
        sysSyncData.setData(jsonStr);
        sysSyncData.setCreateTime(LocalDateTime.now());
        sysSyncData.setError(error);
        return sysSyncData;
    }
}
